package com.tree;

public class Test_1 {
    public static void main(String[] args) {
        int arr[][] = new int[11][11];
        arr[2][5] = 999;
        arr[7][5] = 52;
        arr[8][8] = 25;

        //记录arr非0元素个数
        int count = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if (arr[i][j] != 0) {
                    count++;
                }
            }
        }
            //创建稀疏数组
            int[][] tarr = new int[count + 1][3];
            //设置第一行
            tarr[0][0] = 11;
            tarr[0][1] = 11;
            tarr[0][2] = count;
            //将非0元数传给稀疏数组
            int k = 0;//表示要存入元素稀疏数组的行下标
            for (int i = 0; i < arr.length; i++) {
                for (int j = 0; j < arr.length; j++) {
                    if(arr[i][j]!=0){
                        k++;
                        tarr[k][0]=i;
                        tarr[k][1]=j;
                        tarr[k][2]=arr[i][j];
                    }
                }
            }
            //打印稀疏数组
        for(int[] data :tarr){
            for(int tem:data){
                System.out.print(tem+" ");
            }
            System.out.println("");
        }
        //把稀疏数组改为正常数组
        int newarr[][]=new int[tarr[0][0]][tarr[0][1]];

        for(int i=1;i<tarr.length;i++){
        newarr[tarr[i][0]][tarr[i][1]]=tarr[i][2];
        }
        //打印正常数组
        System.out.println("====正常数组=====");
        for (int i = 0; i <newarr.length ; i++) {
            for (int j = 0; j <newarr.length ; j++) {
                System.out.print(newarr[i][j]+" ");
            }
            System.out.println("");
        }
    }
}

